package pcloud.client.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.PropertyConfigurator;

import pcloud.client.remoteservice.FTP4JService;
import pcloud.client.remoteservice.FileService;

public class GlobalConfig
{
	private static final String localTaskFolderPath = "D:/pcloud";
	private static final String propertiesFileName = "pcloud.properties";
	private static final String propertiesFilePath = "D:/pcloud";
	private static FileService remoteFileService;
	private static final String remoteTaskFolderPath = "/datastore";
	private static GlobalConfig theInstance = null;
	
	private static org.apache.log4j.Logger logger = 
		org.apache.log4j.Logger.getLogger (GlobalConfig.class) ; 

	public static GlobalConfig getInstance()
	{
		if (theInstance == null) {
			theInstance = new GlobalConfig();
		}
		return theInstance;
	}

	public static FileService getRemoteFileService()
	{
		return remoteFileService;
	}

	public static FileService initRemoteService()
	{
		String serverRoot = getInstance().getProperty("serverRoot");
		String user = getInstance().getProperty("user");
		String password = getInstance().getProperty("password");

		GlobalConfig.remoteFileService = FTP4JService.createInstance(
				serverRoot, user, password);
		if (GlobalConfig.remoteFileService == null) {
			logger.error("create remote service failed! ");
		} else {
			logger.info("remote service created! "
					+ GlobalConfig.remoteFileService.toString());
		}
		return GlobalConfig.remoteFileService;
	}

	public static void releaseRemoteService()
	{
		GlobalConfig.remoteFileService.release();
	}

	private String[] localFolders;

	private Properties props = new Properties();

	private String[] remoteFolders;

	private GlobalConfig()
	{
		File file = new File(propertiesFileName);
		if (!file.exists()) {
			file = new File(propertiesFilePath + "/" + propertiesFileName);
			if (!file.exists()) {
				throw new RuntimeException("Configurateion file not found!");
			}
		}
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(file));
			props.load(in);

			initializeLogProperties();

			initializeSyncFolders();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String findLocalSyncRoot(String localPath)
	{
		for (String localSyncFolder : localFolders) {
			if (localPath.toLowerCase().indexOf(localSyncFolder) == 0) {
				return localSyncFolder;
			}
		}
		return null;
	}

	public String findRemoteSyncRoot(String remotePath)
	{
		for (String remoteSyncFolder : remoteFolders) {
			if (remotePath.toLowerCase().indexOf(remoteSyncFolder) == 0) {
				return remoteSyncFolder;
			}
		}
		return null;
	}

	public String[] getLocalFolders()
	{
		return localFolders;
	}

	public String getLocalSyncFolder(String remoteFolder)
	{
		for (int i = 0; i < remoteFolders.length; i++) {
			String aRemoteFolder = remoteFolders[i];
			if (aRemoteFolder.equals(remoteFolder.toLowerCase())) {
				return localFolders[i];
			}
		}
		return null;
	}

	public String getLocalTaskFolderPath()
	{
		return localTaskFolderPath;
	}

	public String getProperty(String key)
	{
		return props.getProperty(key);
	}

	public String[] getRemoteFolders()
	{
		return remoteFolders;
	}

	public String getRemoteSyncFolder(String localFolder)
	{
		for (int i = 0; i < localFolders.length; i++) {
			String aLocalFolder = localFolders[i];
			if (aLocalFolder.equals(localFolder.toLowerCase())) {
				return remoteFolders[i];
			}
		}
		return null;
	}

	public String getRemoteTaskfolderPath()
	{
		return remoteTaskFolderPath;
	}

	private void initializeLogProperties()
	{
		String logFilePath = props.getProperty("logFile");
		if (logFilePath == null) {
			logFilePath = "D:/pcloud/pcloud.log";
		}
		PropertyConfigurator.configure(logFilePath);

	}

	private void initializeSyncFolders()
	{
		String syncFoldersStr = props.getProperty("syncFolder");

		String[] syncFoldersArray = syncFoldersStr.trim().split(",");
		int folderCount = syncFoldersArray.length;
		localFolders = new String[folderCount];
		remoteFolders = new String[folderCount];
		for (int i = 0; i < folderCount; i++) {
			String aSyncFolderStr = syncFoldersArray[i];
			String[] aSyncFolderArray = aSyncFolderStr.trim().split("->");
			if (aSyncFolderArray.length != 2) {
				logger.error(
						"Property file error, sync folder pair does not exist. ["
								+ aSyncFolderStr + "]");
			}
			String aLocalFolder = aSyncFolderArray[0].trim();
			String aRemoteFolder = aSyncFolderArray[1].trim();
			localFolders[i] = aLocalFolder.toLowerCase();
			remoteFolders[i] = aRemoteFolder.toLowerCase();
		}
	}
}
